package com.xbongbong.saas.factory.publicrule;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.domain.entity.ClueRuleEntity;
import com.xbongbong.saas.domain.entity.RuleClueEntity;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author long
 * @version v1.0
 * @date 2020/12/2 19:18
 * @since v1.0
 */
@Component
public class CheckClueRuleGainRate implements ICheckClueRule{

    @Resource
    private PaasRedisHelper paasRedisHelper;

    @Override
    public boolean canGainClue(ClueRuleEntity rule, String corpid,
                               UserEntity userEntity, Long clueId, int dataIdSize, JSONObject data) throws XbbException {

        //没有规则，不限制线索获取
        if(rule == null || Objects.equals(rule.getStatus(), 0)){
            return true;
        }

        //获取用户的每天的捞取、分配频次
        Integer num = StringUtil.toInt(paasRedisHelper.getValue(RedisPrefixConstant.CLUE_GAIN_RATE, corpid + "_" + userEntity.getUserId()), null);
        if(num == null){
            num = getClueGainRateFromDB(rule, userEntity);
            if(num != null){
                Long endTime = DateTimeUtil.getTodayInt() + 86400;
                Long now = DateTimeUtil.getInt();
                long time = endTime - now;
                paasRedisHelper.setValue(RedisPrefixConstant.CLUE_GAIN_RATE, corpid + "_" + userEntity.getUserId(), num, (int) time);
            }
        }
        if(num == null || dataIdSize <= num){
            return true;
        }

        return false;
    }

    @Override
    public List<RuleClueEntity> getBackClueList(UserEntity userEntity,
                                                        ClueRuleEntity rule, List<RuleClueEntity> clueList,
                                                        Integer advanceTime) throws XbbException {
        return new ArrayList<>();
    }

    @Override
    public List<RuleClueEntity> getBackClueList(UserEntity userEntity,
                                                        ClueRuleEntity rule, List<RuleClueEntity> clueList)
            throws XbbException {
        return new ArrayList<>();
    }

    /**
     * 获取数据库中的可捞取、分配线索数
     * @param rule 公海规则捞取频率规则
     * @param userEntity 用户
     * @return
     * @throws XbbException
     */
    public Integer getClueGainRateFromDB(ClueRuleEntity rule, UserEntity userEntity) throws XbbException {
        if(rule == null || Objects.equals(rule.getStatus(), 0)){
            return null;
        }

        /**
         * rule.getRuleType() 必然是 ClueRuleEnum.GAIN_RATE_RULE.getCode()
         */
        IOperateClueRuleValue operateRuleValue = OperateClueRuleValueFactory.operateRuleValueFactory(rule.getRuleType());
        JSONArray ruleValueArray = (JSONArray) operateRuleValue.toObject(rule);

        return CheckRuleUtil.getMinValue(ruleValueArray, userEntity);
    }
}
